unit clsFicha_Compose;

interface

uses dm, dbtables, biblio;

type
  TFicha_Compose = class(TObject)
  public
    Fic_Qtde: double;
    procedure Gravar(p_Pro_Codigo1, p_Pro_Codigo2, p_Pro_Codigo3: string);
    procedure Excluir(p_Pro_Codigo1, p_Pro_Codigo2: string);
    function Recuperar(p_Pro_Codigo1, p_Pro_Codigo2, p_Pro_Codigo3: string): boolean;
    function Recuperar1(p_Pro_Codigo1, p_Pro_Codigo2, p_Pro_Codigo3: string; p_Cont: integer): boolean;
  end;

implementation

{ TFicha_Compose }

procedure TFicha_Compose.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from Ficha_Compose where Pro_Codigo1=:a and Pro_Codigo2=:b');
  tq.params[0].asstring := p_Pro_Codigo1;
  tq.params[1].asstring := p_Pro_Codigo2;
  tq.execsql;
  tq.close;
  tq.Free;
end;

procedure TFicha_Compose.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Ficha_Compose where Pro_Codigo1=:a and Pro_Codigo2=:b and ' +
    'Pro_Codigo3=:c');
  tq.params[0].asstring := p_Pro_Codigo1;
  tq.params[1].asstring := p_Pro_Codigo2;
  tq.params[2].asstring := p_Pro_Codigo3;
  tq.open;
  if tq.eof then begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Insert into Ficha_Compose values (' +
      ':k_Pro_Codigo1,' +
      ':k_Pro_Codigo2,' +
      ':k_Pro_Codigo3,' +
      ':k_Fic_Qtde   )');
  end else begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update Ficha_Compose set ' +
      'Fic_Qtde=:k_Fic_Qtde     ' +
      'where                    ' +
      'Pro_Codigo1=:k_Pro_Codigo1 and ' +
      'Pro_Codigo2=:k_Pro_Codigo2 and ' +
      'Pro_Codigo3=:k_Pro_Codigo3');
  end;
  tq.parambyname('k_Pro_Codigo1').asstring := p_Pro_Codigo1;
  tq.parambyname('k_Pro_Codigo2').asstring := p_Pro_Codigo2;
  tq.parambyname('k_Pro_Codigo3').asstring := p_Pro_Codigo3;
  tq.parambyname('k_Fic_Qtde').asfloat := Fic_Qtde;
  tq.execsql;
  tq.close;
  tq.Free;
end;

function TFicha_Compose.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Ficha_Compose where Pro_Codigo1=:a and Pro_Codigo2=:b and ' +
    'Pro_Codigo3=:c');
  tq.params[0].asstring := p_Pro_Codigo1;
  tq.params[1].asstring := p_Pro_Codigo2;
  tq.params[2].asstring := p_Pro_Codigo3;
  tq.open;
  if tq.eof then result := false else result := true;
  Fic_Qtde := tq.fieldbyname('Fic_Qtde').asfloat;
  tq.close;
  tq.free;
end;

function TFicha_Compose.Recuperar1;
var
  tq: TQuery;
  xcont: integer;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Ficha_Compose where Pro_Codigo1=:a and Pro_Codigo2=:b and ' +
    'Pro_Codigo3=:c');
  tq.params[0].asstring := p_Pro_Codigo1;
  tq.params[1].asstring := p_Pro_Codigo2;
  tq.params[2].asstring := p_Pro_Codigo3;
  tq.open;
  if tq.eof then begin
    result := false;
    tq.close;
    tq.sql.Clear;
    tq.params.clear;
    tq.sql.add('Select * from Ficha_Compose where Pro_Codigo1=:a and Pro_Codigo2<>:b order by Pro_Codigo2,Pro_Codigo3');
    tq.params[0].asstring := p_Pro_Codigo1;
    tq.params[1].asstring := p_Pro_Codigo2;
    tq.open;
    xcont := 0;
    while not tq.eof do begin
      if xcont = p_Cont then begin
        result := true;
        Fic_Qtde := tq.fieldbyname('Fic_Qtde').asfloat;
      end;
      inc(xcont);
      tq.next;
    end;
  end else begin
    result := true;
    Fic_Qtde := tq.fieldbyname('Fic_Qtde').asfloat;
  end;
  tq.close;
  tq.free;
end;

end.
